package com.hzw.controller;

import com.hzw.dto.ApiResult;
import com.hzw.bean.ArticleCategory;
import com.hzw.service.ArticleCategoryService;
import com.hzw.utils.ObjectUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.hzw.api.Api.CLIENT_ERROR;
import static com.hzw.api.Api.SERVER_ERROR;

/**
 * Created by Administrator on 2019/3/28.
 */
@Controller
@CrossOrigin
@RequestMapping("/articleCategory")
public class ArticleCategoryController {
    @Resource
    private ArticleCategoryService articleCategoryService;

    /**
     * 新增文章分类
     * @param articleCategory
     * @return
     */
    @RequestMapping(value = "/addArticleCategory",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult addArticleCategory(@RequestBody ArticleCategory articleCategory){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(articleCategory) || ObjectUtils.isNullOrEmpty(articleCategory.getType())){
            result.error(CLIENT_ERROR,"缺少参数：type");
            return result;
        }else{
            //判断传入的type是否已存在，若已存在则提示换一个type值
            int flag = articleCategoryService.checkExistType(articleCategory.getType());
            if(flag == 1){
                result.error(CLIENT_ERROR,"type值已存在");
                return result;
            }else{
                try{
                    articleCategoryService.addArticleCategory(articleCategory);
                    result.setMsg("文章分类插入成功！");
                }catch (Exception e){
                    result.error(SERVER_ERROR,e.getMessage());
                    return result;
                }
            }
        }
        return result;
    }

    /**
     * 根据Id软删除文章分类
     * @param articleCategory
     * @return
     */
    @RequestMapping(value = "/deleteArticleCategoryById",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult deleteArticleCategoryById(@RequestBody ArticleCategory articleCategory){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(articleCategory) || ObjectUtils.isNullOrEmpty(articleCategory.getId())){
            result.error(CLIENT_ERROR,"缺少参数：id");
            return result;
        }else{
            try{
                String categoryId = articleCategory.getId();
                int count = articleCategoryService.deleteArticleCategoryById(categoryId);
                if(count == 1){
                    result.setMsg("文章分类删除成功！");
                }else if(count == 0){
                    result.setMsg("找不到此id对应的分类，删除失败！");
                }
            }catch (Exception e){
                result.error(SERVER_ERROR,e.getMessage());
                return result;
            }
        }
        return result;
    }

    /**
     * 根据Id更新文章分类
     * @param articleCategory
     * @return
     */
    @RequestMapping(value = "/updateArticleCategoryById",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult updateArticelCategoryById(@RequestBody ArticleCategory articleCategory){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(articleCategory) || ObjectUtils.isNullOrEmpty(articleCategory.getId())){
            result.error(CLIENT_ERROR,"缺少参数：id");
            return result;
        }else{
            try{
                int count = articleCategoryService.updateArticleCategoryById(articleCategory);
                if(count == 0){
                    result.error(SERVER_ERROR,"文章分类更新失败！");
                    return result;
                }else if(count == 1){
                    result.setMsg("文章分类更新成功！");
                }
            }catch (Exception e){
                result.error(SERVER_ERROR,e.getMessage());
                return result;
            }
        }
        return result;
    }

    /**
     * 根据Id查找对应的文章分类
     * @param articleCategory
     * @return
     */
    @RequestMapping(value = "/selectArticleCategoryById",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult selectArticleCategoryById(@RequestBody ArticleCategory articleCategory){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(articleCategory) || ObjectUtils.isNullOrEmpty(articleCategory.getId())){
            result.error(CLIENT_ERROR,"缺少参数：id");
            return result;
        }else{
            try{
                String id = articleCategory.getId();
                ArticleCategory articleCategory1 = articleCategoryService.selectArticleCategoryById(id);
                if(ObjectUtils.isNullOrEmpty(articleCategory1)){
                    result.setMsg("找不到此Id对应的文章分类");
                }else {
                    result.setData(articleCategory1);
                }
            }catch (Exception e){
                result.error(SERVER_ERROR,e.getMessage());
                return result;
            }
        }
        return result;
    }

    /**
     * 查找所有的文章分类
     * @return
     */
    @RequestMapping(value = "/selectAllArticleCategory",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult selectAllArticleCategory(){
        ApiResult result = new ApiResult();
        try{
            List<ArticleCategory> list = articleCategoryService.selectAllArticleCategory();
            if(ObjectUtils.isNullOrEmpty(list)){
                result.setMsg("找不到文章分类！");
            }else {
                result.setData(list);
            }
        }catch (Exception e){
            result.error(SERVER_ERROR,e.getMessage());
            return result;
        }
        return result;
    }
}
